package com.amt.basic.service.impl;

import com.amt.basic.config.ResultMsg;
import com.amt.basic.model.MemberCard;
import com.amt.basic.service.MemberCardService;
import com.amt.util.HttpUtil;
import com.amt.util.JsonUtil;
import com.amt.util.StringUtil;
import com.amt.util.WeiMengApiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class MemberCardServiceImpl implements MemberCardService {
    @Override
    public String memberPointAmountChange(String json) {
        ResultMsg resultMsg = new ResultMsg();
        log.info("会员积分余额订阅消息{}",json);
        Map map = JsonUtil.jsonToHashMap(json);
        String body = (String) map.get("msg_body");
        MemberCard memberCard = (MemberCard) JsonUtil.jsonToObject(body, MemberCard.class);
        //拿到消息  变更的商品
        if(memberCard!=null ){
            //处理积分相关流水
            resultMsg.setErrcode("0");
            resultMsg.setErrmsg("success");
            return JsonUtil.objectToJson(resultMsg);
        }
        resultMsg.setErrcode("0");
        resultMsg.setErrmsg("error");
        return JsonUtil.objectToJson(resultMsg);
    }

    @Override
    public String openMemberCard(String json) {
        ResultMsg resultMsg = new ResultMsg();
        log.info("会员开卡订阅消息{}",json);
        Map map = JsonUtil.jsonToHashMap(json);
        //店铺id
        Long pid = (Long) map.get("pid");
        //客户唯一标识
        Long wid = (Long) map.get("wid");
        //会员卡号
        String memberCode = (String) map.get("memberCode");
        //卡类型
        Integer type = (Integer) map.get("type");
        //开卡门店
        Long storeId = (Long) map.get("storeId");
        //拿到消息 会员卡号 查询开卡信息
        if(StringUtil.isNotEmpty(memberCode)){
            //建卡信息处理
            String memberCardInfoByCode = getMemberCardInfoByCode(wid, storeId);
            log.info("查询开卡会员详情{}",memberCardInfoByCode);
            resultMsg.setErrcode("0");
            resultMsg.setErrmsg("success");
            return JsonUtil.objectToJson(resultMsg);
        }
        resultMsg.setErrcode("0");
        resultMsg.setErrmsg("error");
        return JsonUtil.objectToJson(resultMsg);
    }
    //根据会员卡号查询会员详情
    public String getMemberCardInfoByCode(Long pid,Long storeId){
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("wid",pid);
        hashMap.put("storeId",storeId);
        String result = HttpUtil.requestPost(WeiMengApiUtil.MEMBER_CARD_INFO, JsonUtil.objectToJson(hashMap));
        return result;
    }

}
